package net.nshc.kwkim.mycalc.buttonlistener;

import net.nshc.kwkim.mycalc.*;
import net.nshc.kwkim.mycalc.activity.*;
import net.nshc.kwkim.mycalc.util.*;
import android.app.*;
import android.content.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public class ArithmeticOperatorButtonClickListener extends ButtonClickListener{
	protected ImageButton combination, comma, pow, pow_comma;
	
	public ArithmeticOperatorButtonClickListener(Context context){
		super(context);
	}

	@Override
	public void onClick(View v){
		ImageButton buttonClicked =(ImageButton)v;
		String content = editTextInput.getText().toString();
		
		if(MyCalcActivity.bLog)Log.d("content", "content = "+content);
		
		if (DisplayContentAnalyzer.isTheLastCharacterArithmeticOperator(content)) {
			return;
		}else if (DisplayContentAnalyzer.isTheLastCharacterPoint(content)) {
			appendString(editTextInput, "0");
		}
		
		changeOperator(buttonClicked.getContentDescription().toString());
		
	}

	private void changeOperator(String string) {
		
		String ret = new String();
		String temp = new String();
		
		if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_BUTTONLISTENER, "Input Operator button is "+string);
		
		if(DisplayContentAnalyzer.isTheLastCharacterNumber(editTextInput.getText().toString()) && !editTextInput.getText().toString().equals("")){
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_BUTTONLISTENER, "digit is true");
			ret = "×";
		}
			
		if(string.equals("√")){
			
			ret = ret + "sqrt(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("^")){
			
			ret = ret + "pow(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			pow = (ImageButton)((Activity)context).findViewById(R.id.adbtn);
			pow_comma = (ImageButton)((Activity)context).findViewById(R.id.adcommabtn);
			pow.setVisibility(View.GONE);
			pow_comma.setVisibility(View.VISIBLE);
			
		}else if(string.equals("tan")){
			
			ret = ret + "tan(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("cos")){
			
			ret = ret + "cos(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("sin")){
			
			ret = ret + "sin(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("log")){
			
			ret = ret + "log(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("ln")){
			
			ret = ret + "ln(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("|x|")){
			
			ret = ret + "abs(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("!")){
			
			ret = ret + "fac(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			
		}else if(string.equals("nCm")){
			
			ret = ret + "com(";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			combination = (ImageButton)((Activity)context).findViewById(R.id.combinationbtn);
			comma = (ImageButton)((Activity)context).findViewById(R.id.commabtn);
			combination.setVisibility(View.GONE);
			comma.setVisibility(View.VISIBLE);
			
		}else if(string.equals(",")){
			
			ret = ",";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			combination = (ImageButton)((Activity)context).findViewById(R.id.combinationbtn);
			comma = (ImageButton)((Activity)context).findViewById(R.id.commabtn);
			combination.setVisibility(View.VISIBLE);
			comma.setVisibility(View.GONE);
			
		}else if(string.equals("pow_,")){
			
			ret = ",";
			temp = editTextInput.getText().toString();
			temp = temp + ret;
			pow = (ImageButton)((Activity)context).findViewById(R.id.adbtn);
			pow_comma = (ImageButton)((Activity)context).findViewById(R.id.adcommabtn);
			pow.setVisibility(View.VISIBLE);
			pow_comma.setVisibility(View.GONE);
			
		}else{
			if (DisplayContentAnalyzer.isEmpty(string)){
				return;
			}else{
				ret = string;
				temp = editTextInput.getText().toString();
				temp = temp + ret;
			}
		}
		
		editTextInput.setText(temp);
		editTextInput.setSelection(editTextInput.getText().toString().length());
		
	}
}